package com.kexio.common.dto;

import java.time.LocalDateTime;
import java.util.List;

import com.fasterxml.jackson.annotation.JsonFormat;

/**
 * 会话信息DTO（Sa-Token简化版本）
 * 
 * 🎯 设计原则：
 * 1. 类型安全 - 避免Map的字符串键错误
 * 2. IDE友好 - 自动补全和重构支持  
 * 3. 性能优化 - 避免哈希计算开销
 * 4. 易于维护 - 清晰的字段定义
 * 
 * @author Kexio Team
 * @since 2.2.0 - Sa-Token原生版本
 */
public class SessionInfo {
    
    /** 会话ID */
    private String sessionId;
    
    /** 用户ID */
    private String userId;
    
    /** 用户名 */
    private String username;
    
    /** 昵称 */
    private String nickname;
    
    /** 部门名称 */
    private String deptName;
    
    /** 角色名称列表 */
    private List<String> roleNames;
    
    /** 租户ID */
    private String tenantId;
    
    /** 登录类型（web、mobile等） */
    private String loginType;
    
    /** 客户端IP地址 */
    private String clientIp;
    
    /** 登录地点 */
    private String loginLocation;
    
    /** 用户代理字符串 */
    private String userAgent;
    
    /** 设备信息 */
    private String deviceInfo;
    
    /** 设备类型（desktop、mobile、tablet等） */
    private String deviceType;
    
    /** 操作系统信息 */
    private String osInfo;
    
    /** 浏览器信息 */
    private String browser;
    
    /** 会话创建时间 */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime createTime;
    
    /** 最后活跃时间 */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime lastActiveTime;
    
    /** Token超时时间（秒，-1表示永不超时） */
    private Long tokenTimeout;
    
    // ==================== 构造方法 ====================
    
    public SessionInfo() {
        this.createTime = LocalDateTime.now();
        this.lastActiveTime = LocalDateTime.now();
        this.loginType = "web";
    }
    
    public SessionInfo(String sessionId, String userId) {
        this();
        this.sessionId = sessionId;
        this.userId = userId;
    }
    
    // ==================== Getter & Setter ====================
    
    public String getSessionId() {
        return sessionId;
    }
    
    public void setSessionId(String sessionId) {
        this.sessionId = sessionId;
    }
    
    public String getUserId() {
        return userId;
    }
    
    public void setUserId(String userId) {
        this.userId = userId;
    }
    
    public String getTenantId() {
        return tenantId;
    }
    
    public void setTenantId(String tenantId) {
        this.tenantId = tenantId;
    }
    
    public String getLoginType() {
        return loginType;
    }
    
    public void setLoginType(String loginType) {
        this.loginType = loginType;
    }
    
    public String getClientIp() {
        return clientIp;
    }
    
    public void setClientIp(String clientIp) {
        this.clientIp = clientIp;
    }
    
    public String getLoginLocation() {
        return loginLocation;
    }
    
    public void setLoginLocation(String loginLocation) {
        this.loginLocation = loginLocation;
    }
    
    public String getUserAgent() {
        return userAgent;
    }
    
    public void setUserAgent(String userAgent) {
        this.userAgent = userAgent;
    }
    
    public String getDeviceInfo() {
        return deviceInfo;
    }
    
    public void setDeviceInfo(String deviceInfo) {
        this.deviceInfo = deviceInfo;
    }
    
    public LocalDateTime getCreateTime() {
        return createTime;
    }
    
    public void setCreateTime(LocalDateTime createTime) {
        this.createTime = createTime;
    }
    
    public LocalDateTime getLastActiveTime() {
        return lastActiveTime;
    }
    
    public void setLastActiveTime(LocalDateTime lastActiveTime) {
        this.lastActiveTime = lastActiveTime;
    }
    
    public Long getTokenTimeout() {
        return tokenTimeout;
    }
    
    public void setTokenTimeout(Long tokenTimeout) {
        this.tokenTimeout = tokenTimeout;
    }
    
    public String getUsername() {
        return username;
    }
    
    public void setUsername(String username) {
        this.username = username;
    }
    
    public String getNickname() {
        return nickname;
    }
    
    public void setNickname(String nickname) {
        this.nickname = nickname;
    }
    
    public String getDeptName() {
        return deptName;
    }
    
    public void setDeptName(String deptName) {
        this.deptName = deptName;
    }
    
    public List<String> getRoleNames() {
        return roleNames;
    }
    
    public void setRoleNames(List<String> roleNames) {
        this.roleNames = roleNames;
    }
    
    public String getDeviceType() {
        return deviceType;
    }
    
    public void setDeviceType(String deviceType) {
        this.deviceType = deviceType;
    }
    
    public String getOsInfo() {
        return osInfo;
    }
    
    public void setOsInfo(String osInfo) {
        this.osInfo = osInfo;
    }
    
    public String getBrowser() {
        return browser;
    }
    
    public void setBrowser(String browser) {
        this.browser = browser;
    }
    
    // ==================== 辅助方法 ====================
    
    /**
     * 检查Token是否即将过期（剩余时间少于5分钟）
     */
    public boolean isTokenNearExpiry() {
        return tokenTimeout != null && tokenTimeout > 0 && tokenTimeout < 300;
    }
    
    /**
     * 更新最后活跃时间为当前时间
     */
    public void updateLastActiveTime() {
        this.lastActiveTime = LocalDateTime.now();
    }
    
    /**
     * 根据User-Agent判断是否为移动设备
     */
    public boolean isMobileDevice() {
        if (userAgent == null) return false;
        String ua = userAgent.toLowerCase();
        return ua.contains("mobile") || ua.contains("android") || ua.contains("iphone") || ua.contains("ipad");
    }
    
    /**
     * 获取会话持续时间（分钟）
     */
    public long getSessionDurationMinutes() {
        if (createTime == null || lastActiveTime == null) return 0;
        return java.time.Duration.between(createTime, lastActiveTime).toMinutes();
    }
    
    @Override
    public String toString() {
        return "SessionInfo{" +
                "sessionId='" + sessionId + '\'' +
                ", userId='" + userId + '\'' +
                ", tenantId='" + tenantId + '\'' +
                ", loginType='" + loginType + '\'' +
                ", clientIp='" + clientIp + '\'' +
                ", createTime=" + createTime +
                ", lastActiveTime=" + lastActiveTime +
                ", tokenTimeout=" + tokenTimeout +
                '}';
    }
}
